package cn.wit.summit.chwan.utils;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import cn.wit.summit.chwan.interfaces.OnIsDownloadInterface;
import cn.wit.summit.chwan.models.response.AccountLoginRespone;
import cn.wit.summit.chwan.models.response.ChcekIsDownloadResponse;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import retrofit2.Retrofit;

/**
 * Created by yangg on 2018/4/20.
 */

public class MobileCodeUtils {


    /**
     * 获取登录的验证码
     * @param activity
     * @param iphone
     * @return
     */
    public static void getCode(final Activity activity, String iphone) {
        Log.e("TAG", "手机号码：" + iphone);
        if (NetWorkUtils.isNetworkConnected(activity)) {
            RetrofitManager retrofitManager = RetrofitManager.getInstance(activity);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.getMobileCode(iphone, "reg").enqueue(new MyCallback<ResponseBase<AccountLoginRespone>>() {
                @Override
                public void onSuccess(ResponseBase<AccountLoginRespone> model) {
                    ToastUtils.getInstance(activity).showToastSystem("验证码发送成功");
                }

                @Override
                public void onFailure(int code, String msg) {
                    ToastUtils.getInstance(activity).showToastSystem(msg);
                }
            });
        } else {
            ToastUtils.getInstance(activity).showToastSystem("网络连接失败，再试试吧~");
        }
    }

    //获取修改密码的验证码
    public static void getChangePWDCode(final Activity activity, String iphone) {
        Log.e("TAG", "手机号码：" + iphone);
        if (NetWorkUtils.isNetworkConnected(activity)) {
            RetrofitManager retrofitManager = RetrofitManager.getInstance(activity);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.getMobileCode(iphone, "passwd").enqueue(new MyCallback<ResponseBase<AccountLoginRespone>>() {
                @Override
                public void onSuccess(ResponseBase<AccountLoginRespone> model) {
                    ToastUtils.getInstance(activity).showToastSystem("验证码发送成功");
                }

                @Override
                public void onFailure(int code, String msg) {
                    ToastUtils.getInstance(activity).showToastSystem(msg);
                }
            });
        } else {
            ToastUtils.getInstance(activity).showToastSystem("网络连接失败，再试试吧~");
        }
    }

    /**
     * 任务-启动app标记
     * @param context

     * @return
     */
    public static void taskStartAppTag(Context context) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
            if (uid==0) {
                return;
            }
            String token = AccountUtil.getInstance().getToken();

            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.taskStartAppTag(uid, token).enqueue(new MyCallback<ResponseBase>() {
                @Override
                public void onSuccess(ResponseBase model) {
                }

                @Override
                public void onFailure(int code, String msg) {
                }
            });
        }
    }

    /**
     * 任务-下载游戏标记
     * @param context
     * @param type 1:启动下载 2:下载完成 3:启动游戏
     * @return
     */
    public static void taskDownloadGameTag(Context context, String gameId, int type) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
            if (uid==0) {
                return;
            }
            String token = AccountUtil.getInstance().getToken();
            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.taskDownloadGameTag(uid, token, gameId, type).enqueue(new MyCallback<ResponseBase>() {
                @Override
                public void onSuccess(ResponseBase model) {
                }

                @Override
                public void onFailure(int code, String msg) {
                }
            });
        }
    }

    /**
     * 任务-下载游戏标记
     * @param context
     * @return
     */
    public static void taskShareTag(Context context) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
            if (uid==0) {
                return;
            }
            String token = AccountUtil.getInstance().getToken();
            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.taskShareTag(uid, token).enqueue(new MyCallback<ResponseBase>() {
                @Override
                public void onSuccess(ResponseBase model) {
                }

                @Override
                public void onFailure(int code, String msg) {
                }
            });
        }
    }

    /**
     * 任务-下载游戏标记
     * @param context
     * @return
     */
    public static void taskShowDialogTag(Context context, int type) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
            if (uid==0) {
                return;
            }
            String token = AccountUtil.getInstance().getToken();
            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.taskShowDialogTag(uid, token, type).enqueue(new MyCallback<ResponseBase>() {
                @Override
                public void onSuccess(ResponseBase model) {
                }

                @Override
                public void onFailure(int code, String msg) {
                }
            });
        }
    }

    /**
     * 任务-消息已读标记
     * @param context
     * @return
     */
    public static void taskReadNotifyTag(Context context, String id) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
            if (uid==0) {
                return;
            }
            String token = AccountUtil.getInstance().getToken();
            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.taskReadNotifyTag(uid, token, id).enqueue(new MyCallback<ResponseBase>() {
                @Override
                public void onSuccess(ResponseBase model) {
                }

                @Override
                public void onFailure(int code, String msg) {
                }
            });
        }
    }

    /**
     * 任务-是否下过该游戏
     * @param context
     * @return
     */
    public static void checkDownloadedGame(final Context context, String gameId, final OnIsDownloadInterface mOnIsDownloadInterface) {
        if (NetWorkUtils.isNetworkConnected(context)) {
            int uid = AccountUtil.getInstance().getUid();
//            if (uid==0) {
//                mOnIsDownloadInterface.onResult();
//                long nowTime = PreferenceUtil.getLongPreferences(context, PreferenceUtil.PRE_FIRST_DOWNLOAD);
//                AccountBean mAccountBean = AccountUtil.getInstance().getAccountData();
//                if (!DateUtils.isToday(nowTime) && (
//                        (mAccountBean==null) || (mAccountBean!=null && mAccountBean.getDownMoney()>0))) {
////                    MessageMoneyDialog mMessageMoneyDialog = new MessageMoneyDialog(context);
////                    mMessageMoneyDialog.show();
////                    mMessageMoneyDialog.setShowType(2);
//                }
//                return;
//            }
            String token = AccountUtil.getInstance().getToken();
            RetrofitManager retrofitManager = RetrofitManager.getInstance(context);
            Retrofit retrofitLogin = retrofitManager.getLoginRetrofit();
            ApiUtil apiLogin = retrofitLogin.create(ApiUtil.class);
            apiLogin.checkFirstDownload(uid, token).enqueue(new MyCallback<ResponseBase<ChcekIsDownloadResponse>>() {
                @Override
                public void onSuccess(ResponseBase<ChcekIsDownloadResponse> model) {
                    ChcekIsDownloadResponse response = model.getData_info();
                    LogUtils.e("tbl>>>response.getIsDownload():"+response.getIsDownload());
                    if (response.getIsDownload()==1) {
                    }
                    mOnIsDownloadInterface.onResult();
                }

                @Override
                public void onFailure(int code, String msg) {
                    mOnIsDownloadInterface.onResult();
                }
            });
//            apiLogin.checkDownloadedGame(uid, token, gameId).enqueue(new MyCallback<ResponseBase<ChcekIsDownloadResponse>>() {
//                @Override
//                public void onSuccess(ResponseBase<ChcekIsDownloadResponse> model) {
//                    ChcekIsDownloadResponse response = model.getData_info();
//                    if (response.getIsDownload()!=1) {
//                        long nowTime = PreferenceUtil.getLongPreferences(context, PreferenceUtil.PRE_FIRST_DOWNLOAD);
//                        AccountBean mAccountBean = AccountUtil.getInstance().getAccountData();
//                        if (!DateUtils.isToday(nowTime) && (
//                                (mAccountBean==null) || (mAccountBean!=null && mAccountBean.getDownMoney()>0))) {
////                            MessageMoneyDialog mMessageMoneyDialog = new MessageMoneyDialog(context);
////                            mMessageMoneyDialog.show();
////                            mMessageMoneyDialog.setShowType(2);
//                        }
//                    }
//                    mOnIsDownloadInterface.onResult();
//                }
//
//                @Override
//                public void onFailure(int code, String msg) {
//                    mOnIsDownloadInterface.onResult();
//                }
//            });
        }
    }


}
